from rest_framework import serializers

from bowen.models import Bowen


class BowenSerializer(serializers.ModelSerializer):
    fenlei_display = serializers.SerializerMethodField()
    class Meta:
        model = Bowen
        fields = ('id','fenlei_display','title','postdate','fenlei','imgurl','zishu')

    def get_fenlei_display(self, obj):
        # 获取fenlei字段的选项描述
        return dict(Bowen.fenlei_choices).get(obj.fenlei, '2')


class BowenContentSerializer(serializers.ModelSerializer):
    fenlei_display = serializers.SerializerMethodField()
    class Meta:
        model = Bowen
        fields = ('id','fenlei_display','title','postdate','content','fenlei','zishu')

    def get_fenlei_display(self, obj):
        # 获取fenlei字段的选项描述
        return dict(Bowen.fenlei_choices).get(obj.fenlei, '2')